﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraCharts;
using Lib.Utils;

namespace MS.INV.Reports.Profits
{
    public partial class Profit_Chart_Compare : Lib.Bases.Forms.BaseForm
    {
        DataTable Data = new DataTable();

        public Profit_Chart_Compare()
        {
            InitializeComponent();
            barYear.EditValue = DateTime.Now.Year;
            barYear2.EditValue = DateTime.Now.Year - 1;
        }

        private void btRef_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                pivot.DataSource = DBLayer.ExecDataTable("Rpt_ProfitChartCompare", "Command,Year1,Year2", "Summary",
                    Cvert.ToInt16(barYear.EditValue, 0), Cvert.ToInt16(barYear2.EditValue, 0));
                Application.DoEvents();

                DataTable DT1 = DBLayer.ExecDataTable("rpt_ProfitChartYear", "Command,Year", "Summary", Cvert.ToInt16(barYear.EditValue, 0));
                Application.DoEvents();
                DataTable DT2 = DBLayer.ExecDataTable("rpt_ProfitChartYear", "Command,Year", "Summary", Cvert.ToInt16(barYear2.EditValue, 0));
                Data = GetNewDT();

                chart.Series.Clear();

                // Add a bar series to it.
                Series series1 = new Series("Series 1", ViewType.Bar);
                Series series2 = new Series("Series 2", ViewType.Bar);

                decimal value = 0;
                for (int i = 1; i <= 12; i++)
                {
                    value = GetValue(DT1, i);
                    series1.Points.Add(new SeriesPoint(i, value));
                    Data.Rows[i - 1]["Thang"] = i;
                    Data.Rows[i - 1]["Nam1"] = value;
                    value = GetValue(DT2, i);
                    series2.Points.Add(new SeriesPoint(i, value));
                    Data.Rows[i - 1]["Nam2"] = value;
                }

                chart.Series.AddRange(new Series[] { series1, series2 });
                ((BarSeriesLabel)series1.Label).Visible = true;

                series1.LegendText = Convert.ToInt32(barYear.EditValue).ToString();
                series2.LegendText = Convert.ToInt32(barYear2.EditValue).ToString();

                series1.PointOptions.ValueNumericOptions.Format = NumericFormat.Number;
                series1.PointOptions.ValueNumericOptions.Precision = 0;
                
                series2.PointOptions.ValueNumericOptions.Format = NumericFormat.Number;
                series2.PointOptions.ValueNumericOptions.Precision = 0;

                XYDiagram diagram = (XYDiagram)chart.Diagram;
                diagram.AxisY.NumericOptions.Format = NumericFormat.Number;
                diagram.AxisY.NumericOptions.Precision = 0;
            }
            catch (Exception ex)
            {
                Lib.Bases.SysError.RaiseError(ex, this.Name, "Load data");
            }
        }

        private DataTable GetNewDT()
        {
            DataTable DT = new DataTable();
            DT.Columns.Add(new DataColumn("Thang", Type.GetType("System.Int32")));
            DT.Columns.Add(new DataColumn("Nam1", Type.GetType("System.Decimal")));
            DT.Columns.Add(new DataColumn("Nam2", Type.GetType("System.Decimal")));
            for (int i = 1; i <= 12; i++)
            {
                DataRow row = DT.NewRow();
                row["Thang"] = i;
                row["Nam1"] = 0;
                row["Nam2"] = 0;
                DT.Rows.Add(row);
            }

            return DT;
        }

        private decimal GetValue(DataTable DT, int Month)
        {
            DataRow[] rS = DT.Select("Thang=" + Month);
            if (object.Equals(rS, null) || rS.Length <= 0)
                return 0;
            return Cvert.ToDecimal(rS[0]["LoiNhuan"], 0);
        }

        private void btExcel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            using (Exports.Profits.Profit_Chart_Compare cls = new Exports.Profits.Profit_Chart_Compare())
            {
                cls.DoExport(Data, Cvert.ToInt16(barYear.EditValue, 0), Cvert.ToInt16(barYear2.EditValue, 0));
            }

            if (System.IO.File.Exists(Application.StartupPath + @"\Export Xls\Profit_Chart_Compare.xls"))
                Lib.Utils.Uti.RunFile(Application.StartupPath + @"\Export Xls\Profit_Chart_Compare.xls");
        }

        private void btClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }
    }
}
